package com.fable.web.controller.customer;

import com.fable.biz.operate.CustomerTypeService;
import com.fable.biz.operate.dto.CustomerTypeDTO;
import com.fable.biz.operate.query.CustomerTypeQueryCondition;
import com.fable.common.config.RequireLogin;
import com.fable.common.enums.BusiErrCode;
import com.fable.common.utils.JsonResult;
import com.fable.utils.StringUtil;
import com.fable.web.page.P;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * 客户类型
 * @ClassName CustomerTypeController
 * @Description TODO
 * @Author aaters
 * @Date 2018/7/18 上午12:26
 * @Version 1.0
 **/

@Controller
@RequireLogin
@RequestMapping("/customerType")
public class CustomerTypeController {

    @Autowired
    private CustomerTypeService customerTypeService;


    /**
     * 客户类型列表
     *
     * @return
     */
    @RequestMapping("/customerTypeList.htm")
    public String customerType(ModelMap modelMap, CustomerTypeQueryCondition condition) {
        List<CustomerTypeDTO> list = customerTypeService.queryCustomerTypeList(condition);
        modelMap.put("customerTypeList", list);
        modelMap.put("query", condition);
        return P.Common.customerTypeList;
    }


    /**
     * 跳转修改或添加页面
     *
     * @return
     */
    @RequestMapping("/addOrModifyCustomerType.htm")
    public String addCustomerType(ModelMap modelMap, Integer id) {
        modelMap.put("customerType", customerTypeService.queryCustomerById(id));
        return P.Common.addCustomerType;
    }

    /**
     * 添加或修改客户类型
     *
     * @param customerTypeDTO
     * @return
     */
    @RequestMapping("/addOrModifyCustomerType.json")
    @ResponseBody
    public JsonResult addOrModifyCustomerType(@ModelAttribute CustomerTypeDTO customerTypeDTO) {
        JsonResult jsonResult = new JsonResult();
        if (StringUtil.isBlank(customerTypeDTO.getTypeName()) && StringUtil.isBlank(customerTypeDTO.getTypeDescribe())) {
            jsonResult.setErrCode(BusiErrCode.PARAM_ERROR);
        }
        int count = customerTypeService.addOrModifyCustomerType(customerTypeDTO);
        if (count == 0) {
            jsonResult.setErrCode(BusiErrCode.INNER_ERROR);
        }
        return jsonResult;
    }

    /**
     * 逻辑删除用户成功
     *
     * @return
     */
    @RequestMapping("/delCustomerType.json")
    @ResponseBody
    public JsonResult delCustomerType(String id) {
        JsonResult jsonResult = new JsonResult();
        if (StringUtil.isEmail(id)) {
            jsonResult.setErrCode(BusiErrCode.PARAM_ERROR);
            return jsonResult;
        }
        CustomerTypeDTO customerTypeDTO = new CustomerTypeDTO();
        customerTypeDTO.setId(Integer.valueOf(id));
        customerTypeDTO.setStatus(-1);
        customerTypeDTO.setGmtModified(new Date());
        int count = customerTypeService.delCustomerType(customerTypeDTO);
        if (count != 1) {
            jsonResult.setErrCode(BusiErrCode.INNER_ERROR);
        }
        return jsonResult;
    }
}
